// pages/add/add.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      setIndex:"-1",
      setName:"",
      setPhone:"",
      setBigAddress:"",
      setDetAddress:"",
      setDefult:false
  },

  switchChange(){
    let state = this.data.setDefult
    if(state){
      this.setData({
        setDefult:false
      })
    }else{
      let myAddress = wx.getStorageSync("myAddress");
        myAddress.forEach(item=>{
            console.log(item);
            item.defult = false;
        })
      wx.setStorageSync('myAddress',myAddress);
      this.setData({
        setDefult:true
      })

    }
    
  },

  newName(options){
    let values = options.detail.value;
    this.setData({
      setName:values
    })
  },

  newDetAddress(options){
    let values = options.detail.value;
    this.setData({
      setDetAddress:values
    })
  },
  newPhone(options){
    let values = options.detail.value;
    values = values.replace(/^\d{15,17}$/, ""); 
    this.setData({
      setPhone:values
    })
  },
  newBigAddress(options){
    let values = options.detail.value;
    this.setData({
      setBigAddress:values
    })
  },
  saveAddress(){
      let myAddress = wx.getStorageSync("myAddress");
      let len = wx.getStorageSync("myAddress").length;
      let ind = this.data.setIndex;
      console.log('12',ind);
      // ind为空报错
      if(ind != -1){
        let list = {
          id:len,
          name:this.data.setName,
          phone:this.data.setPhone,
          bigAddress:this.data.setBigAddress,
          detAddress:this.data.setDetAddress,
          defult:this.data.setDefult
        }
        if(list.name == "" || list.phone == "" || list.bigAddress == "" || list.detAddress == ""){
          wx.showModal({
            cancelColor: 'cancelColor',
            title:"请填写完整信息!"
          })
          var flag = false;
        }else{
          var flag = true;
          myAddress[ind].name = this.data.setName;
          myAddress[ind].phone = this.data.setPhone;
          myAddress[ind].bigAddress = this.data.setBigAddress;
          myAddress[ind].detAddress = this.data.setDetAddress;
          myAddress[ind].defult = this.data.setDefult;
        }
      }else{
        let list = {
          id:len,
          name:this.data.setName,
          phone:this.data.setPhone,
          bigAddress:this.data.setBigAddress,
          detAddress:this.data.setDetAddress,
          defult:this.data.setDefult
        }
        if(list.name == "" || list.phone == "" || list.bigAddress == "" || list.detAddress == ""){
          wx.showModal({
            cancelColor: 'cancelColor',
            title:"请填写完整信息!"
          })
          var flag = false;
        }else{
          myAddress.push(list);
          var flag = true;
        }
      }
      wx.setStorageSync('myAddress',myAddress);
      if(flag){
        wx.navigateBack();
      }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      // if(options.id == item.id){
      //   this.setData({
      //     add: item
      //   })
      // }
      console.log(options);
      let myAddress = wx.getStorageSync("myAddress");
      if(options.id>=0){
        this.setData({
          setIndex:myAddress[options.id].id,
          setName:myAddress[options.id].name,
          setPhone:myAddress[options.id].phone,
          setBigAddress:myAddress[options.id].bigAddress,
          setDetAddress:myAddress[options.id].detAddress,
          setDefult:myAddress[options.id].defult
        })
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})